package ac.manchester.cs.afzal.autoservd.vector_handler;

import java.util.Iterator;
import java.util.Vector;
import java.util.StringTokenizer;

/**
 *
 * @author hamafz
 */
public class Vector_Builder
{

    Vector<String> vector1;

    /**
     *
     */
    public Vector_Builder()
    {
    }
    

    /**
     * Converts the given string into vector, using the separator
     * @param contents
     * @param seperator
     * @return
     */
    public Vector<String> string_to_vector(String contents, String seperator)
    {
        vector1 = new Vector();
        StringTokenizer st1 = new StringTokenizer(contents, seperator);
        while(st1.hasMoreElements())
        {
            String term = st1.nextToken();
            term.toLowerCase();
            term.trim();
            vector1.add(term);
        }
        return vector1;
    }

    /**
     * Converts the vector into a string, with a each item of vector added into a new line
     * @param occurrences_vector
     * @return
     */
    public String vector_to_string_with_items_list(Vector<String> occurrences_vector)
    {
        return vector_to_string(occurrences_vector,System.getProperty("line.separator"));
    }


    /**
     * Converts the vector into a string, with a each item of vector separated by given separator
     * @param occurrences_vector
     * @param seperator
     * @return
     */
    public String vector_to_string(Vector<String> occurrences_vector,String seperator)
    {
        StringBuffer strBuf = new StringBuffer();
        for(String vector_item : occurrences_vector)
        {
            vector_item = vector_item.trim();
            strBuf.append(vector_item);
            strBuf.append(seperator);
        }
        return strBuf.toString();
    }

    /**
     *
     * @param contents
     * @param seperator
     * @return
     */
    public Vector exclusive_keyword_to_vector(String contents, String seperator)
    {
        vector1 = new Vector();
        StringTokenizer st1 = new StringTokenizer(contents, seperator);
        while(st1.hasMoreElements())
        {
            String term = st1.nextToken();
            term.toLowerCase();

            if(vector1.contains(term) || term.equals(" [") || term.equals(" ]") || term.equals(" "));
            else
            {
                vector1.add(term);
            }
        }
        return vector1;
    }

    /**
     *
     * @param contents
     * @param seperator
     * @return
     */
    public Vector occurences_keyword_to_vector(String contents, String seperator)
    {
        vector1 = new Vector();
        StringTokenizer st1 = new StringTokenizer(contents, seperator);
        while(st1.hasMoreElements())
        {
            String term = st1.nextToken();
            term.toLowerCase();

            if(term.startsWith(" "))
            {
                vector1.add(term.substring(1, term.length()));
            }
            else
            {
                vector1.add(term);
            }
        }
        return vector1;
    }

        /**
     *
     * @param str
     * @return
     */
    public Vector term_profiles(String[] str)
    {
        Vector set1 = new Vector();
        String setElement;
        int i = 0;
        int j = 2;

        if (str.length == 1)
        {
            set1.add(str[0]);
            return set1;
        }
        else
        {
            try
            {
                while (i < str.length)
                {
                    set1.add(str[i]);
                    i++;
                }
                i = 0;
                while (j < str.length)
                {
                    while (j > i)
                    {
                        setElement = str[i] + " " + str[i + 1];
                        set1.add(setElement.toString());
                        i++;
                        setElement = "";
                    }
                    j++;
                }
            }
            catch (Exception ex)
            {
                System.out.println("Value of Exception in term profiles is : " + ex.getStackTrace());
            }
        }
        return set1;
    }

    /**
     *
     * @param str
     * @return
     */
    public Vector term_profiles(Vector str)
    {
        Vector set1 = new Vector();
        String setElement;
        int i = 0;
        int j = 2;

        if (str.size() == 1)
        {
            set1.add(str.elementAt(0).toString());
            return set1;
        }
        else
        {
            try
            {
                while (i < str.size())
                {
                    set1.add(str.elementAt(i).toString());
                    i++;
                }
                i = 0;
                while (j < str.size())
                {
                    while (j > i)
                    {
                        setElement = str.elementAt(i) + " " + str.elementAt(i + 1).toString();
                        set1.add(setElement.toString());
                        i++;
                        setElement = "";
                    }
                    j++;
                }
            }
            catch (Exception ex)
            {
                System.out.println("Value of Exception in term profiles is : " + ex.getStackTrace());
            }
        }
        return set1;
    }
    /**
     * This function converts the entries into a csv format which is used to
     * build the SQL query afterwards. Suitable for only small vectors
     *
     * @param vector_iterms
     * @return
     */
    public String convert_vector_to_csv(Vector<String> vector_iterms)
    {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("'");

        for (String vector_item:vector_iterms)
        {
            strBuf.append(vector_item);
            strBuf.append("','");
        }
        String csv_string = strBuf.toString();
        csv_string = csv_string.substring(0,csv_string.length()-2);
        return csv_string;
    }

}
